#include "TutorApp7.h"

#ifdef WIN32
#ifdef Tutorial7_EXPORTS
#define TUTORAPP7_API __declspec(dllexport)
#else
#define TUTORAPP7_API __declspec(dllimport)
#endif
#else
#define TUTORAPP7_API
#endif

extern "C" TUTORAPP7_API Blobz::BaseApplication* GetInstance()
{
	Tutorial7::TutorApp7* pTutorial = new Tutorial7::TutorApp7();
	return (Blobz::BaseApplication*)pTutorial;
}

extern "C" TUTORAPP7_API unsigned int GetMajorReleaseVersion()
{
	return NUI_RELEASE_MAJOR_VER;
}

extern "C" TUTORAPP7_API unsigned int GetMinorReleaseVersion()
{
	return NUI_RELEASE_MINOR_VER;
}

extern "C" TUTORAPP7_API const char* GetAppName()
{
	return "Tutorial7";
}

namespace Tutorial7
{
	
	/**
	* Constructor
	*/
	TutorApp7::TutorApp7():
		mAspectRatio(1.0f),
		mGUIManager(),
		mpAddButton(NULL),
		mpRemoveButton(NULL),
		mFont(),
		mStatusText(""),
		mCornerMenuNames()
	{
	}
	
	/**
	* Destructor
	*/
	TutorApp7::~TutorApp7()
	{
	}

	/**
	* Loads and setups up all the required objects and gui elements
	*/
	bool TutorApp7::onInitialize()
	{
		// Tell the application to use a fixed aspect ratio so that our view content is not stretched
		getWindow().setUseFixedRatio(true); 

		// Save the application directory for easier access
		QString appDir = getApplicationDirectory();
		
		// save the application window aspect ratio
		mAspectRatio = getWindow().getWindowAspect();

		// load the background texture
		mBackgroundTexture.load(getApplicationDirectory()+"bg.png", true);

		//setup the buttons
		mpAddButton = mGUIManager.addButton("AddButton", appDir+"greenUnpress.png", appDir+"greenPress.png", "Add Corner Button");
		mpAddButton->setSize(QVector2D(0.1f, 0.075f));
		mpAddButton->setPosition(QVector2D(mAspectRatio/2.f-0.3f, 0.7f));

		mpRemoveButton = mGUIManager.addButton("RemoveButton", appDir+"greenUnpress.png", appDir+"greenPress.png", "Remove Corner Button");
		mpRemoveButton->setSize(QVector2D(0.1f, 0.075f));
		mpRemoveButton->setPosition(QVector2D(mAspectRatio/2.f+0.3f, 0.7f));

		// setup the font 
		mFont.loadDefaultSystemFont();
		mFont.setRenderScale(0.0005f);

		// Status text
		mStatusText = "Press Add Button to add a button to corner menu.";

		// listen to gui manager's button events
		mGUIManager.addListener(this);

		// listen to corner menu button events
		getWindow().addMenuListener(this);

		return true;
	}
	
	/**
	* On Reload Handler
	*/
	void TutorApp7::onReload()
	{
	}
	
	/**
	* Unload all loaded objects
	*/
	void TutorApp7::onDeinitialize()
	{
		getWindow().removeMenuListener(this);

		// unload all objects
		mGUIManager.unload();
		mFont.unload();
		mBackgroundTexture.unload();
	}
	
	/**
	* Update event handler
	*/
	void TutorApp7::onUpdate(float inFrameTime)
	{
		// send update events to all objects
		mGUIManager.update(inFrameTime);
	}
	
	/**
	* Render event handler
	*/
	void TutorApp7::onDraw()
	{
		// tell Snowflake that we want to start rendering
		getWindow().enableRendering();

		// screen setup, part of the Snowflake framework
		// clears the background to white
		getWindow().defaultRenderSetup(QVector4D(1,1,1,1));

		// Render the background texture
		const QVector2D windowSize(getWindow().getWindowAspect(), 1.f);		
		const QVector2D mPosition(0.f, 0.f);

		// Enables alpha blending
		glEnable(GL_BLEND);
		glDisable(GL_DEPTH_TEST);
		glEnable(GL_TEXTURE_2D);

		// Binds the texture ID to OpenGL so it can be used
		glBindTexture(GL_TEXTURE_2D, mBackgroundTexture.getID()); 
		// Begin drawing GLQuads
		glBegin(GL_QUADS);
		glColor4f(1, 1, 1, 1);
		glTexCoord2f(0,1);
		glVertex2f(mPosition.x(), mPosition.y());
		glTexCoord2f(1,1);
		glVertex2f(mPosition.x()+windowSize.x(), mPosition.y());
		glTexCoord2f(1,0);
		glVertex2f(mPosition.x()+windowSize.x(), mPosition.y()+windowSize.y());
		glTexCoord2f(0,0);
		glVertex2f(mPosition.x(), mPosition.y()+windowSize.y());
		glEnd();
		glDisable(GL_TEXTURE_2D);

		glColor4f(1,1,1,1);

		// render the app's buttons
		mGUIManager.render();

		// render the text at the center of the screen
		glColor4f(0,0,0,1);
		mFont.print(mStatusText, mAspectRatio/2.f, 0.5f);
		glColor4f(1,1,1,1);

		// tell Snowflake that we're done rendering
		getWindow().disableRendering();
	}

	/**
	* Triggered when there is a finger down event on the screen
	* \param inData holds the touch information
	*/
	void TutorApp7::onFingerDown(const TouchListener::TouchData& inData)
	{
		mGUIManager.onFingerDown(inData);
	}

	/**
	* Triggered when there is a finger up event on the screen
	* \param inData holds the touch information
	*/
	void TutorApp7::onFingerUp(const TouchListener::TouchData& inData)
	{
		mGUIManager.onFingerUp(inData);
	}

	/**
	* Triggered when there is a finger update event on the screen
	* \param inData holds the touch information
	*/
	void TutorApp7::onFingerUpdate(const TouchListener::TouchData& inData)
	{
		mGUIManager.onFingerUpdate(inData);
	}

	/**
	* Triggered when there is a finger moves out of the screen or application window
	* \param inData holds the touch information
	*/
	void TutorApp7::onFingerExit(const TouchListener::TouchData& inData)
	{
		// just treat finger exit events as finger up events
		onFingerUp(inData);
	}

	/**
	* Triggered when there is a GUI event from the GUIManager that the application is listening to
	* \param inpComponent A pointer to the GUI component
	* \param inGUIEvent The GUI event type
	* \param inFingerEvent The finger event (up/down/update) associated with the GUI event
	*/
	void TutorApp7::onGUIEvent(Blobz::GUIBase* inpComponent, Blobz::GUIEvent inGUIEvent, Blobz::FingerEvent inFingerEvent)
	{
		if(inGUIEvent == Blobz::Button_Released)
		{
			// add a new button to the corner menu
			if(inpComponent == mpAddButton)
			{
				// create a unique name of this new button
				QString newName = QString("CornerButton")+QString::number(mCornerMenuNames.size());

				// add the new button to the corner menu
				getWindow().addMenuButton(newName, getApplicationDirectory()+"corner.png", getApplicationDirectory()+"cornerPressed.png");

				// also add the new button name to our list for cross reference when deleting and button press detection
				mCornerMenuNames.append(newName);
			}
			// remove the last added button from the corner menu
			else if(inpComponent == mpRemoveButton)
			{
				// if there are no button then exit
				if(mCornerMenuNames.size() == 0)
					return;
				
				// fetch the last name from the list of buttons
				QString lastItem = mCornerMenuNames.at(mCornerMenuNames.size()-1);

				// remove the last item from the list
				mCornerMenuNames.pop_back();
				
				// remove the button from the corner menu
				getWindow().removeMenuButton(lastItem);				
			}
			else
			{
				// check if the button is present in the list
				if(mCornerMenuNames.contains(inpComponent->getName()))
				{
					// change the status message
					mStatusText = "Corner Button:"+inpComponent->getName()+" selected.";
				}
			}
		}
	}
}
